<template>
  <el-drawer
    title="商品分类"
    ref="drawer"
    :visible.sync="drawer">
    <el-checkbox
      :indeterminate="isIndeterminate"
      v-model="checkAll"
      @change="handleCheckAllChange"
     >全选</el-checkbox>
    <el-checkbox-group
      v-model="targetKinds"
      @change="handleTargetKindsChange">
      <el-checkbox
        v-for="item in kinds"
        :label="item.id"
        :key="item.kind"
      >{{item.kind}}</el-checkbox>
    </el-checkbox-group>
  </el-drawer>
</template>
<script>
    export default {
        name: "kinds_drawer",
      props:['type'],
      data() {
        return {
          drawer:false,
          checkAll: true,
          kinds: [],
          kindsId:[],
          targetKinds:[],
          isIndeterminate: true
        };
      },
      methods: {
          //全选
        handleCheckAllChange(val){
          this.targetKinds = val ? this.kindsId:[];
          this.isIndeterminate = false;
          this.findWithKind();
        },
        //筛选
        handleTargetKindsChange(value){
          let checkedCount = value.length;
          this.checkAll = (checkedCount === this.kinds.length);
          this.isIndeterminate=checkedCount > 0 && checkedCount<this.kinds.length;
          this.findWithKind();
          },
          //商品分类查询
        findWithKind(){
          switch (this.type){
            case 'GOOD':this.$store.commit("good/findWithKind",this.targetKinds);break;
            case 'INVENTORY':this.$store.commit("inventory/findWithKind",this.targetKinds);break;
            default:break;
          }

        },
        //获取种类列表
        loadKinds(){
          var _this=this
          this.$axios.get('/kinds').then(resp => {
            if (resp && resp.status === 200) {
              for(var i=0;i<resp.data.length;i++){
                var temp={
                  id:'',
                  kind:''
                }
                //过滤空白数据
                if(resp.data[i].kind==null)
                  continue
                temp.id=resp.data[i].id;
                temp.kind=resp.data[i].kind;
                _this.kinds.push(temp);
                _this.kindsId.push(temp.id);
              }
            }
          })
        }
      },
      mounted() {
          this.loadKinds();
          this.targetKinds=this.kindsId;
      }
    }
</script>

<style scoped>

</style>
